#!/usr/bin/perl
use Getopt::Std;
#getopt("d:a:e:t", \%args);
getopt("d:t", \%args);
my $maindir = $args{d};
my $td = $args{t}; 
#my $ambifile = $args{a};
#my $excludehapmap= $args{e};
#my $maintped= $args{t};
chomp $maindir;
$ambifile = "ambi_hapmap_processed_beagle_input.tped";
$excludehapmap= "excluded_no_hapmap_processed_beagle_input.tped";
$maintped="unprocessed_beagle_input.tped";
#chomp $ambifile;
#chomp $excludehapmap;
#chomp $maintped;
#print $maindi."\n";
#if($ambifile eq "")
#{
#        die "entered ambifile name  is empty\n";

#}
#if($excludehapmap eq "")
#{
#        die "entered excludehapmap file name  is empty\n";

#}
#if($maintped eq "")
#{
#        die "entered maintped name is empty\n";

#}
if($maindir eq "")
{
        die "entered dir name is empty\n";

}
if($td  eq "")
{
        die "entered temp dir name is empty\n";

}

$dic = $maindir;
$maindir = "$maindir/temp/$td";
#merging chr1-22 and x imputation output results
system("cat $maindir/whole_geno_phased $maindir/23/beagle_out_female.geno_female_male.bgl.gz.phased $maindir/whole_no_snp_hapmap.txt>$maindir/whole_geno_1_22_x_nosnphapmap");
#creating the tped file
system("/usr/bin/perl $dic/bin/perl_create_beagle_output_plink_tped.pl -m $dic/bin/files/newhapmap3_sample_run.tped -i $maindir/whole_geno_1_22_x_nosnphapmap -o $maindir/whole_geno_1_22_x_nosnphapmap.tped");
#merging imputed output file with excluded hapmap snps
system("cat $maindir/whole_geno_1_22_x_nosnphapmap.tped  $maindir/$excludehapmap > $maindir/whole_geno_1_22_x_nosnphapmap_nohapmapsnpsInitalI.tped");
#system("mv $maindir/whole_geno_1_22_x_nosnphapmap_nohapmapsnpsInital.tped $maindir/whole_geno_1_22_x_nosnphapmap_nohapmapsnpsInitalI.tped");
#remove if any duplicate snp ids are present
#system("/usr/bin/perl $dic/bin/perl_extract_uniq.pl $maindir/whole_geno_1_22_x_nosnphapmap_nohapmapsnpsInitalI.tped $maindir/whole_geno_1_22_x_nosnphapmap_nohapmapsnpsInital.tped");
system("mv $maindir/whole_geno_1_22_x_nosnphapmap_nohapmapsnpsInitalI.tped $maindir/whole_geno_1_22_x_nosnphapmap_nohapmapsnpsInital.tped");
#system("rm $maindir/whole_geno_1_22_x_nosnphapmap_nohapmapsnpsInitalI.tped");
#check the strand direction of ambiguos snps and replace the output with original if present
system("/usr/bin/perl $dic/bin/perl_check_ambi_snp.pl -a $maindir/$ambifile -i $maindir/whole_geno_1_22_x_nosnphapmap_nohapmapsnpsInital.tped -o $maindir/whole_geno_1_22_x_nosnphapmap_nohapmapsnpsInital_check_ambi1.tped");
#create combine dosage
$sys = "/usr/bin/perl $dic/bin/perl_create_combine_dosage.pl -d $maindir -g $maindir/whole_gprobs -t $maindir/whole_geno_1_22_x_nosnphapmap_nohapmapsnpsInital_check_ambi1.tped -f $maindir/23/female/beagleout_female.geno_female.bgl.gz.gprobs -m $maindir/23/male/beagleout_male.geno_male.bgl.gz.phased";
#print $sys."\n";
system($sys);
#check the strand direction for the dosage
system("/usr/bin/perl $dic/bin/perl_change_gprobes_ambi_snp.pl $maindir/$ambifile $maindir/whole_chr_gprobs_dosage3 $maindir/whole_chr_gprobs_dosage_ambi");
#creating the indicator file
system("/usr/bin/perl $dic/bin/perl_create_indicator_file.pl $maindir $maindir/whole_r2 $maindir/$maintped $maindir/$ambifile $maindir/$excludehapmap $maindir/whole_no_snp_hapmap.txt $maindir/whole_geno_1_22_x_nosnphapmap_nohapmapsnpsInital_check_ambi1.tped $maindir/whole_chr_gprobs_dosage_ambi");
$syste = "/usr/bin/perl $dic/bin/perl_create_new_map_tpedfile_4sort.pl $maindir/whole_geno_1_22_x_nosnphapmap_nohapmapsnpsInital_check_ambi1.tped $maindir/snp_order.txt $maindir";
print $syste."\n";
system($syste);
system("/usr/bin/perl $dic/bin/perl_inputlist_sortfile.pl $maindir/snp_order.txt $maindir/whole_chr_gprobs_dosage_ambi 1 $maindir/sorted_whole_chr_gprobs_dosage_ambi");
system("/usr/bin/perl $dic/bin/perl_inputlist_sortfile.pl $maindir/snp_order.txt $maindir/whole_ind  1 $maindir/sorted_whole_ind");
system("/usr/bin/perl $dic/bin/perl_inputlist_sortfile_tped.pl $maindir/snp_order.txt  $maindir/whole_geno_1_22_x_nosnphapmap_nohapmapsnpsInital_check_ambi1.tped 2 $maindir/sorted_whole_geno.map"); 
